[id].tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { Head } from "fresh/runtime";
  2. import { HttpError } from "fresh";
  3. import { page, type PageProps } from "fresh";
  4. import { define } from "utils/state.ts";
  5. import { checkToken } from "utils/server.ts";
  6. import { find } from "utils/db.ts";
  7. import TopBar from "../islands/TopBar.tsx";
  8. import Editor, { EditorMode } from "../islands/Editor.tsx";
  9. interface PostProps {
  10. id: string;
  11. title: string;
  12. content: string;
  13. shared: boolean;
  14. isLogined: boolean;
  15. allowMode: EditorMode;
  16. }
  17. export const handler = define.handlers({
  18. GET(ctx) {
  19. const tokenUserId = checkToken(ctx.req);
  20. const postId = ctx.params.id;
  21. const post = find(
  22. "Post",
  23. tokenUserId
  24. ? { id: postId, user_id: tokenUserId }
  25. : { id: postId, shared: 1 },
  26. ["title", "content", "shared"],
  27. );
  28. if (post.length > 0) {
  29. return page({
  30. id: postId,
  31. isLogined: Boolean(tokenUserId),
  32. allowMode: tokenUserId ? EditorMode.Both : EditorMode.Read,
  33. title: post[0]["title"] as string,
  34. content: post[0]["content"] as string,
  35. shared: post[0]["shared"] === 1,
  36. });
  37. }
  38. throw new HttpError(404);
  39. },
  40. });
  41. export default function Post(props: PageProps<PostProps>) {
  42. return (
  43. <>
  44. <Head>
  45. <title>{props.data.title}</title>
  46. </Head>
  47. <div className="pd-page">
  48. <TopBar
  49. id={props.data.id}
  50. title={props.data.title}
  51. shared={props.data.shared}
  52. allowMode={props.data.allowMode}
  53. isLogined={props.data.isLogined}
  54. />
  55. <Editor
  56. id={props.data.id}
  57. content={props.data.content}
  58. allowMode={props.data.allowMode}
  59. />
  60. </div>
  61. </>
  62. );
  63. }